package com.managementSystem.client.presenter;


import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.HasKeyUpHandlers;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.managementSystem.client.StaffBeanServiceAsync;
import com.managementSystem.client.event.MainMenu;
import com.managementSystem.client.event.RegisterStaff;
import com.managementSystem.client.presenter.LoginPresenter.Display;
import com.managementSystem.shared.StaffBean;

public class LoginPresenter implements Presenter  {
	StaffBean currentUser;
	public interface Display {
		HasClickHandlers getLoginButton();
		HasClickHandlers getRegisterButton();
		HasKeyUpHandlers getUserNameComplete();
		HasKeyUpHandlers getPasswordComplete();
		HasValue<String> getUserName();
		HasValue<String> getPassword();
		Widget asWidget();
	}
	private final StaffBeanServiceAsync rpcService;
	private final HandlerManager eventBus;
	private final Display display;

	public LoginPresenter(StaffBeanServiceAsync rpcService, HandlerManager eventBus, Display view) {
		this.rpcService = rpcService;
		this.eventBus = eventBus;
		this.display = view;
	}

	public void bind() {
		display.getLoginButton().addClickHandler(new ClickHandler() {   
			public void onClick(ClickEvent event) {
				checkLogin();
				if (currentUser!=null) {
					
				}
			}
		});

		display.getRegisterButton().addClickHandler(new ClickHandler() {   
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(new RegisterStaff());
			}
		});

		display.getUserNameComplete().addKeyUpHandler(new KeyUpHandler() {   
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					checkLogin();
				}
			}

		});

		display.getPasswordComplete().addKeyUpHandler(new KeyUpHandler() {   
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					checkLogin();
				}
			}

		});
	}

	@Override
	public void go(HasWidgets container) {
		bind();
		container.clear();
		container.add(display.asWidget());
	}

	private void checkLogin() {
		String userName=display.getUserName().getValue();
		String passWord=display.getPassword().getValue();
		rpcService.authenticateUser(userName, passWord, new AsyncCallback<StaffBean>() {
			public void onSuccess(StaffBean result) {
				currentUser=result;
				if (result==null) {
					Window.alert("Incorrect Username or Password");
					
				} else {
					eventBus.fireEvent(new MainMenu());
					
				}
			}
			public void onFailure(Throwable caught) {
				Window.alert("Error fetching contact details");
			}
		});
	}
}
